package com.frejoys.store.controller;

import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.aop.VerifyStore;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.app.AddUserCashDto;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.dto.store.*;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.CommonUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.app.StoreWalletLogListVo;
import com.frejoys.common.vo.app.UserWalletVo;
import com.frejoys.common.vo.common.UserCashInfoVo;
import com.frejoys.common.vo.store.StoreCashListVo;
import com.frejoys.common.vo.store.StoreWalletVo;
import com.frejoys.service.CommonService;
import com.frejoys.service.StoreWalletLogService;
import com.frejoys.service.StoreWalletService;
import com.frejoys.service.UserCashService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "店铺钱包", description = "店铺钱包")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixStore)
@Authorization(Authority.Private)
public class MallStoreWalletController {

    private final StoreWalletLogService storeWalletLogService;

    private final StoreWalletService storeWalletService;

    private final CommonService commonService;

    private final UserCashService userCashService;

    private final RedissonClient redissonClient;


//     @PostMapping("/userWallet/userWalletInfo")
//     @Operation(summary = "用户钱包信息")
//     public Result<UserWalletVo> userWalletInfo(){
//         return Result.success(userWalletService.userWalletInfo(RequestUtil.userId()));
//     }

    // 待注释
//    @PostMapping("/storeWalletLog/walletLogPage")
//    @Operation(summary = "获取店铺钱包记录")
//    public Result<List<StoreWalletLogListVo>> walletLogPage(@RequestBody @Validated StoreIdAndPageDto dto) {
//        return Result.success(storeWalletLogService.storeWalletListInfo(dto));
//    }

    @PostMapping("/storeWalletLog/getStoreWalletInfo")
    @Operation(summary = "店铺钱包")
    public Result<StoreWalletVo> getStoreWalletInfo(@RequestBody @Validated IdAndPageDto dto){
        return Result.success(storeWalletService.getStoreWalletInfo(dto));
    }

    @VerifyStore
    @PostMapping("/storeWalletLog/cash")
    @Operation(summary = "店铺申请提现")
    public Result storeWalletLogCash(@RequestBody @Validated StoreCashDto dto) {
        commonService.limitationMoney(dto.getMoney());
        // 保留2位小数
        dto.setMoney(CommonUtil.bigDecimalRoundDown(dto.getMoney()));
        storeWalletLogService.walletCash(RequestUtil.userId(),dto);
        return Result.success();
    }


    @PostMapping("/storeWalletLog/addUserCash")
    @Operation(summary = "添加提现账号信息")
    public Result addUserCash(@RequestBody @Validated AddUserCashDto dto){
        RLock lock = redissonClient.getLock(CacheKeyUtil.userCashLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                userCashService.addUserCash(dto,RequestUtil.userId());
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }

    @PostMapping("/storeWalletLog/userCashInfo")
    @Operation(summary = "查询提现账号信息")
    public Result<UserCashInfoVo> userCashInfo(){
        return Result.success(userCashService.userCashInfo(RequestUtil.userId()));
    }


    @PostMapping("/storeWalletLog/storeCashListInfo")
    @Operation(summary = "店铺钱包记录列表")
    public Result<List<StoreCashListVo>> storeCashListInfo(@RequestBody @Validated StoreCashListDto dto) {
        return Result.success(storeWalletLogService.storeCashListInfo(dto));
    }


}
